# -*- coding: utf-8 -*-
from openerp.osv import fields
from openerp.osv import osv
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
ISODATEFORMAT = '%Y-%m-%d'
ISODATETIMEFORMAT = "%Y-%m-%d %H:%M:%S"

class hr_employee(osv.osv):
    _inherit = "hr.employee"
    _decription = u'保持关联用户和个人收款账户一致'

    def onchange_user(self, cr, uid, ids, user_id, context=None):
        value = super(hr_employee, self).onchange_user(cr, uid, ids, user_id, context=context)

        for user in self.pool.get('res.users').browse(cr, uid, user_id, context=context):
            value['value'].update({'address_home_id': user.partner_id})

        return value
    _columns = {
        'entry_date': fields.date(u'入职时间'),
        'pre_employment_seniority': fields.integer(u'入职前工龄')
    }
    _defaults = {
        'pre_employment_seniority': 0
    }

    def create_employee_annual(self, cr, uid, context=None):
        employee_ids = self.search(cr, uid, [], context=context)
        annual_obj = self.pool.get('annual.leave.days.rule')
        overtime_obj = self.pool.get('work.overtime.leave')
        for employee in self.browse(cr, uid, employee_ids, context=context):
            if employee.entry_date:
                entry_date = datetime.strptime(employee.entry_date, ISODATEFORMAT)
                now_date = datetime.now()
                all_relativedelta = relativedelta(now_date, entry_date)
                if entry_date.month > now_date.month or \
                                        entry_date.month == now_date.month and entry_date.day > now_date.day:
                    continue
                if all_relativedelta.years < 1:
                    continue
                years = all_relativedelta.years + employee.pre_employment_seniority
                annual_ids = annual_obj.search(cr, uid, [('min_year', '<=', years), ('max_year', '>', years)], context=context)
                annual_row = annual_obj.browse(cr, uid, annual_ids, context=context)
                if not annual_ids:
                    continue
                annual_data = self.get_annual_data(cr, uid, annual_row.annual_days, employee.id, employee.user_id.id,
                                                   entry_date,  context=context)
                if not overtime_obj.search(cr, uid, [('age', '=', annual_data.get('age')),
                                                     ('type', '=', 'annual_leave'),
                                                     ('user_id', '=', annual_data.get('user_id'))], context=context):
                    overtime_obj.create(cr, uid, annual_data, context=context)

    def get_annual_data(self, cr, uid, annual_days, employee_id, user_id, entry_date, context=None):
        data_dcit = {}
        start_date_time = "%s-%s-%s" % (datetime.now().year, entry_date.month, entry_date.day)
        end_date = datetime.now() + relativedelta(years=1)
        end_date_time = "%s-%s-%s" % (end_date.year, entry_date.month, entry_date.day)
        data_dcit.update({'employee_id': employee_id, 'type': 'annual_leave', 'is_approve': True,
                          'approver_user_id': uid, 'user_id': user_id, 'age': end_date.year,
                          'hour_qty': annual_days * 8, 'approve_time': fields.datetime.now(),
                          'note': u'系统统一生成年假', 'start_time': start_date_time,
                          'expire_date': end_date_time,
                          'end_time': end_date_time})
        return data_dcit

    def create_annual(self, cr, uid, ids, context=None):
        self.create_employee_annual(cr, uid, context=context)